package com.ruoyi.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.domain.Asset;
import com.ruoyi.service.AssetService;
import com.ruoyi.utils.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.net.URISyntaxException;

@RestController
@RequestMapping("/asset")
public class AssetController {

    @Autowired
    AssetService assetService;

    //添加资产信息
    @PostMapping("/addAsset")
    public R addAsset(@RequestBody Asset asset){
        assetService.addAsset(asset);
       return R.ok();
    }

    //编辑资产信息
    @PutMapping("/editAsset")
    public R editAsset(@RequestBody Asset asset){
        assetService.updateAsset(asset);
        return R.ok();
    }

    //删除资产信息
    @DeleteMapping("/delete/{assetNo}")
    public R delete(@PathVariable("assetNo") String assetNo){
        assetService.deleteAssetByAssetNo(assetNo);
        return R.ok();
    }

    //联合条件查询资产信息
    @GetMapping("/select")
    public R select(int page, int limit, String assetNo, String assetName, String responsibilityDepartmentName, String period){
        if((assetNo != null && !assetNo.isEmpty())||
                (assetName != null && !assetName.isEmpty())||
                (responsibilityDepartmentName != null && !responsibilityDepartmentName.isEmpty())||
                (period != null && !period.isEmpty())){
            return R.ok(assetService.selectAsset(page,limit,assetNo,assetName,responsibilityDepartmentName,period));
        } else{
            return R.ok(assetService.selectAllAsset(page,limit));
        }
    }
/*

    //打印资产信息
    @PostMapping("printAsset/{assetNo}")
    public R printAsset(@PathVariable("assetNo") String assetNo, HttpServletRequest request){
        Asset asset = assetService.selectAssetByAssetNo(assetNo);

        //生成二维码
        String content ="资产编号："+assetNo+"\n资产名称："+asset.getAssetName()+"\n责任部门："+asset.getResponsibilityDepartmentName()+"\n取得日期："+asset.getGetDate();
        QRCodeUtil.create(content,assetNo);

        URI uri = null;
        try {
            uri = getHost(new URI(request.getRequestURL() + ""));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

        String host = uri.getHost();
        int port = uri.getPort();
        String url = String.format("""
                http://%s:%s/images/%s
                """,host,port,assetNo+".png");

        // 将字符串转换为JSON
        JSONObject jsonContent = new JSONObject();
        jsonContent.put("assetNo", assetNo);
        jsonContent.put("assetName", asset.getAssetName());
        jsonContent.put("responsibilityDepartmentName", asset.getResponsibilityDepartmentName());
        jsonContent.put("getDate", asset.getGetDate());
        jsonContent.put("url",url.trim());
        return R.ok(jsonContent);

    }

    private URI getHost(URI uri) {
    // 检查输入的 URI 是否为 null
    if (uri == null) {
        return null;
    }
    URI effectiveURI;
    try {
        // 创建一个新的 URI 对象，仅包含协议、用户信息、主机名和端口号
        effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
                null, null, null);
    } catch (URISyntaxException e) {
        // 记录异常信息，方便调试
        System.err.println("Failed to create URI: " + e.getMessage());
        effectiveURI = null;
    }
    return effectiveURI;

}
*/


}